package labox.innovation.gameserver.model.entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.db.core.L2DatabaseFactory;
import labox.innovation.gameserver.idfactory.IdFactory;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;

/**
 * @author evill33t
 *
 */
public class Couple {

	private static final Logger _log = LoggerFactory.getLogger(Couple.class.getName());

	// =========================================================
	// Data Field
	private long _Id;

	private long _player1Id;

	private long _player2Id;

	private boolean _maried;

	private Calendar _affiancedDate;

	private Calendar _weddingDate;

	// =========================================================
	// Constructor
	public Couple(long coupleId) {
		_Id = coupleId;

		Connection con = null;
		try {
			PreparedStatement statement;
			ResultSet rs;

			con = L2DatabaseFactory.getInstance().getConnection();

			statement = con.prepareStatement("Select * from mods_wedding where id = ?");
			statement.setLong(1, _Id);
			rs = statement.executeQuery();

			while (rs.next()) {
				_player1Id = rs.getInt("player1Id");
				_player2Id = rs.getInt("player2Id");
				_maried = rs.getBoolean("married");

				_affiancedDate = Calendar.getInstance();
				_affiancedDate.setTimeInMillis(rs.getLong("affianceDate"));

				_weddingDate = Calendar.getInstance();
				_weddingDate.setTimeInMillis(rs.getLong("weddingDate"));
			}
			statement.close();
		} catch (Exception e) {
			_log.error("Exception: Couple.load(): ", e);
		} finally {
			try {
				con.close();
			} catch (Exception e) {
			}
		}
	}

	public Couple(FuzePcInstance player1, FuzePcInstance player2) {
		long _tempPlayer1Id = player1.getObjectId();
		long _tempPlayer2Id = player2.getObjectId();

		_player1Id = _tempPlayer1Id;
		_player2Id = _tempPlayer2Id;

		_affiancedDate = Calendar.getInstance();
		_affiancedDate.setTimeInMillis(Calendar.getInstance().getTimeInMillis());

		_weddingDate = Calendar.getInstance();
		_weddingDate.setTimeInMillis(Calendar.getInstance().getTimeInMillis());

		Connection con = null;
		try {
			con = L2DatabaseFactory.getInstance().getConnection();
			PreparedStatement statement;
			_Id = IdFactory.getInstance().getNextId(IdFactory.ID_COUPLE);
			statement = con.prepareStatement("INSERT INTO mods_wedding (id, player1Id, player2Id, married, affianceDate, weddingDate) VALUES (?, ?, ?, ?, ?, ?)");
			statement.setLong(1, _Id);
			statement.setLong(2, _player1Id);
			statement.setLong(3, _player2Id);
			statement.setBoolean(4, false);
			statement.setLong(5, _affiancedDate.getTimeInMillis());
			statement.setLong(6, _weddingDate.getTimeInMillis());
			statement.execute();
			statement.close();
		} catch (Exception e) {
			_log.error("creat couple error", e);
		} finally {
			try {
				con.close();
			} catch (Exception e) {
			}
		}
	}

	public void marry() {
		Connection con = null;
		try {
			con = L2DatabaseFactory.getInstance().getConnection();
			PreparedStatement statement;

			statement = con.prepareStatement("UPDATE mods_wedding set married = ?, weddingDate = ? where id = ?");
			statement.setBoolean(1, true);
			_weddingDate = Calendar.getInstance();
			statement.setLong(2, _weddingDate.getTimeInMillis());
			statement.setLong(3, _Id);
			statement.execute();
			statement.close();
			_maried = true;
		} catch (Exception e) {
			_log.error("update couple error", e);
		} finally {
			try {
				con.close();
			} catch (Exception e) {
			}
		}
	}

	public void divorce() {
		Connection con = null;
		try {
			con = L2DatabaseFactory.getInstance().getConnection();
			PreparedStatement statement;

			statement = con.prepareStatement("DELETE FROM mods_wedding WHERE id=?");
			statement.setLong(1, _Id);
			statement.execute();
			statement.close();
		} catch (Exception e) {
			_log.error("Exception: Couple.divorce(): ", e);
		} finally {
			try {
				con.close();
			} catch (Exception e) {
			}
		}
	}

	public final long getId() {
		return _Id;
	}

	public final long getPlayer1Id() {
		return _player1Id;
	}

	public final long getPlayer2Id() {
		return _player2Id;
	}

	public final boolean getMaried() {
		return _maried;
	}

	public final Calendar getAffiancedDate() {
		return _affiancedDate;
	}

	public final Calendar getWeddingDate() {
		return _weddingDate;
	}
}
